package StackAndQueue;

import java.util.ArrayDeque;
import java.util.LinkedList;

public class EvalRPN150 {
    public static void main(String[] args) {
        System.out.println(evalRPN(new String[]{"2","1","/","3","*"}));
    }
    public static int evalRPN(String[] tokens) {
        ArrayDeque<Integer> array = new ArrayDeque<>();
        int i = 0;
        while( i < tokens.length){
            if(tokens[i].equals("+")){
                array.push(array.pop() + array.pop());
            }else if(tokens[i].equals("-")){
                array.push(-array.pop() + array.pop());
            }else if(tokens[i].equals("*")){
                array.push(array.pop() * array.pop());
            }else if(tokens[i].equals("/")){
                int temp1 = array.pop();
                int temp2 = array.pop();
                array.push(temp2 / temp1);
            }else{
                array.push(Integer.valueOf(tokens[i]));
            }
            i++;
        }
        return array.pop();
    }
}
